# _*_ coding: utf-8 _*_
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError


class SaleOrder(models.Model):
    _inherit = "sale.order"

    @api.multi
    @api.depends('order_line')
    def _compute_confirm_visible(self):
        for order in self:
            for line in order.order_line:
                if line.standard_price != line.price_unit:
                    order.confirm_visible = False
                    break
            order.confirm_visible = True

    confirm_visible = fields.Boolean(compute=_compute_confirm_visible, default=True, store=True)

    @api.multi
    def action_confirm(self):
        for order in self:
            order.err_mess = u""
            if not order.pass_credit:
                for line in order.order_line:
                    if line.price_unit == 0 or line.standard_price == 0:
                        order.err_mess += line.product_id.display_name + u' 单价或标准价不可以为0，请调整价格,或请求特批！\n'
                    if line.price_unit != line.standard_price:
                        order.err_mess += line.product_id.display_name + u' 单价和标准价不同，请调整价格,或请求特批！\n'
        return super(SaleOrder, self).action_confirm()
